package xyz.kongbai121.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.kongbai121.demo.domain.ResourcesShare;
import xyz.kongbai121.demo.domain.RespBean;
import xyz.kongbai121.demo.service.IResourcesShareService;

import javax.servlet.http.HttpSession;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * 资源共享处理控制器
 * @author 空白
 */
@Controller
@RequestMapping("/resources")
public class ResourcesShareController {

    @Autowired
    private IResourcesShareService shareService;
    @Autowired
    private StringRedisTemplate redisTemplate;

//    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session, Model model) {
        if (username == null || password == null) return "login";
        String pass = DigestUtils.md5DigestAsHex((password + username).getBytes());
        if (username.equals("admin") && pass.equals("3f04ff643df0ec0b857a58916aca2ad7")) {
            session.setAttribute("token", "3f04ff643df0ec0b857a58916aca2ad7");
            return "res";
        }
        model.addAttribute("msg", "账号密码错误");
        return "login";
    }

//    @RequestMapping("/save")
    public String save(HttpSession session, Model model, String head, String body, String from) {
        String sessionToken = (String) session.getAttribute("token");
        if (sessionToken == null) return "login";
        if (!sessionToken.equals("3f04ff643df0ec0b857a58916aca2ad7")) return "login";
        try {
            if (head != null && !head.isEmpty() && body != null && !body.isEmpty() && from != null && !from.isEmpty()) {
                String msg = shareService.save(head, body, from);
                model.addAttribute("msg", msg);
                return "res";
            }
            model.addAttribute("msg", "表单数据不能为空");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "失败");
        }
        return "res";
    }

    @RequestMapping("/loginByAjax")
    @ResponseBody
    public RespBean loginByAjax(String username, String password) {
        if (username == null || password == null) return new RespBean(false,"请先登录");
        String pass = DigestUtils.md5DigestAsHex((password + username).getBytes());
        if (username.equals("admin") && pass.equals("3f04ff643df0ec0b857a58916aca2ad7")) {
            String token = UUID.randomUUID().toString().replaceAll("-","");
            //TODO 生成令牌并返回给用户
            redisTemplate.opsForValue().set(token,"3f04ff643df0ec0b857a58916aca2ad7",30, TimeUnit.MINUTES);
            return new RespBean(true,token);
        }
        return new RespBean(false,"账号密码错误");
    }

    @RequestMapping("/saveByAjax")
    @ResponseBody
    public RespBean saveByAjax(String head, String body, String from, String token) {
        //TODO 登录验证
        String sessionToken = redisTemplate.opsForValue().get(token);
        if (sessionToken == null) return new RespBean(false,"请先登录");
        if (!sessionToken.equals("3f04ff643df0ec0b857a58916aca2ad7")) return new RespBean(false,"请先登录");
        //TODO 添加数据
        if (head != null && !head.isEmpty() && body != null && !body.isEmpty() && from != null && !from.isEmpty()) {
            shareService.save(head, body, from);
            return new RespBean(true,"添加成功");
        }
        return new RespBean(true,"表单数据不能为空");
    }

    @RequestMapping("/list")
    @ResponseBody
    public RespBean list(String title, Integer current, Long pageSize) {
        if (current == null || current < 1) current = 1;
        if (pageSize == null) pageSize = 10L;
        //分页对象
        Page<ResourcesShare> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(current);
        if (title != null && !title.isEmpty()) {
            //清除前后空格,中间的空格用百分号代替
            String head = title.trim().replaceAll(" ", "%");
            return new RespBean(true, shareService.list("%" + head + "%", page));
        }
        return new RespBean(true, shareService.list(null, page));
    }

    @RequestMapping("/get")
    @ResponseBody
    public RespBean get(Long id) {
        return new RespBean(true, shareService.get(id));
    }
}
